package com.cnpc.framework.base.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.cnpc.framework.base.entity.Role;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.RoleService;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 角色列表
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    private String list() {

        return "base/auth/role_list";
    }

    /**
     * 角色编辑
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/edit")
    private String eidt(String id, HttpServletRequest request) {

        request.setAttribute("id", id);
        return "base/auth/role_edit";
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public Role get(String id) {

        Role role = roleService.get(Role.class, id);
        return role;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(Role role) {

        roleService.saveOrUpdate(role);
        return new Result(true);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id) {

        return roleService.delete(id);
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public Result getList(@RequestParam(value = "name", required = false) String name,
                          @RequestParam(value = "pageNum", required = false) Integer pageNum,
                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Result result = null;
        try {
            result = roleService.getList(name, pageNum, pageSize);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new Result();
            result.setMessage("系统繁忙！");
        }
        return result;
    }

    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserList(@RequestParam("roleId") String roleId,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "select", required = false) String select,
                              @RequestParam(value = "pageNum", required = false) Integer pageNum,
                              @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Result result = null;
        try {
            result = roleService.getUserList(roleId, name, select, pageNum, pageSize);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new Result();
            result.setMessage("系统繁忙！");
        }
        return result;
    }


}
